﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;

namespace CustSign
{
    public partial class frmPrizeT : Form
    {
        Int32 intState, intCardID, intCardNum;
        ArrayList PanGroup, PrizeGroup;
        Random CardID;

        public frmPrizeT()
        {
            InitializeComponent();
        }

        private void frmPrizeT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (TCardScroll.Enabled == true)
                {
                    TCardScroll.Enabled = false;

                    if (intState == 0)
                    {
                        lblPrizeTCard1.Text = NewPrize();
                        lblCardScroll.Text = lblPrizeTCard1.Text;
                    }
                    else if (intState == 1)
                    {
                        lblPrizeTCard2.Text = NewPrize();
                        lblCardScroll.Text = lblPrizeTCard2.Text;
                    }
                    else if (intState == 2)
                    {
                        lblPrizeTCard3.Text = NewPrize();
                        lblCardScroll.Text = lblPrizeTCard3.Text;
                    }                    
                    else
                    {
                    }
                }
                else
                {
                    TCardScroll.Enabled = true;
                }
            }

            if (e.KeyChar == 27)
            {
                this.Dispose();
            }
        }

        private void frmPrizeT_Load(object sender, EventArgs e)
        {
            FontBase FontNow = new FontBase();

            FontNow.FontGet();

            this.Top = 0;
            this.Left = 0;
            this.Width = Screen.GetWorkingArea(this).Width;
            this.Height = Screen.GetWorkingArea(this).Height;
            intState = 0;

            this.BackgroundImage = Properties.Resources.imageback;

            lblPrizeTCard1.Text = "                ";
            lblPrizeTCard2.Text = "                ";
            lblPrizeTCard3.Text = "                ";            

            lblPrizeT.ForeColor = FontNow.cColor;
            lblPrizeT.Font = FontNow.fFont;

            lblPrizeTCard1.ForeColor = FontNow.cColor;
            lblPrizeTCard1.Font = FontNow.fFont;

            lblPrizeTCard2.ForeColor = FontNow.cColor;
            lblPrizeTCard2.Font = FontNow.fFont;

            lblPrizeTCard3.ForeColor = FontNow.cColor;
            lblPrizeTCard3.Font = FontNow.fFont;

            lblCardScroll.Font = FontNow.fFont;

            lblPrizeT.Top = (Int32)Math.Round(this.Height * 0.1823);
            lblCardScroll.Top = (Int32)Math.Round(this.Height * 0.888 - lblCardScroll.Height);

            lblPrizeTCard1.Top = (lblPrizeT.Top * 3 + lblCardScroll.Top) / 4;
            lblPrizeTCard2.Top = (lblPrizeT.Top + lblCardScroll.Top) / 2;
            lblPrizeTCard3.Top = (lblPrizeT.Top + lblCardScroll.Top * 3) / 4;

            lblPrizeT.Left = (Int32)(this.Width - lblPrizeT.Width) / 2;
            lblPrizeTCard1.Left = (Int32)(this.Width - lblPrizeTCard1.Width) / 2;
            lblPrizeTCard2.Left = (Int32)(this.Width - lblPrizeTCard2.Width) / 2;
            lblPrizeTCard3.Left = (Int32)(this.Width - lblPrizeTCard3.Width) / 2;
            lblCardScroll.Left = (Int32)(this.Width - lblCardScroll.Width) / 2; ;

            PanGroup = new ArrayList();
            PrizeGroup = new ArrayList();

            OleDbConnection Conn = new OleDbConnection(Properties.Settings.Default.strOleConn);

            OleDbCommand Comm = Conn.CreateCommand();
            OleDbDataReader CustRead;
            Conn.Open();

            Comm.CommandText = "SELECT DISTINCT TSigns.Pan FROM ((TSigns INNER JOIN TCustList ON TSigns.Pan = TCustList.Pan) LEFT OUTER JOIN TPrizes ON TSigns.Pan = TPrizes.Pan) WHERE (TPrizes.Pan IS NULL)";
            CustRead = Comm.ExecuteReader();

            if (CustRead.HasRows)
            {
                while (CustRead.Read())
                {
                    PanGroup.Add(CustRead["Pan"]);
                }
            }
            else
            {
                PanGroup.Add("8888888888888881");
                PanGroup.Add("8888888888888882");
                PanGroup.Add("8888888888888883");
                PanGroup.Add("8888888888888885");
                PanGroup.Add("8888888888888886");
                PanGroup.Add("8888888888888887");
                PanGroup.Add("8888888888888888");
                PanGroup.Add("8888888888888889");
                PanGroup.Add("8888888888888890");
            }

            CustRead.Close();

            CustRead.Dispose();
            Comm.Dispose();
            Conn.Close();
            Conn.Dispose();

            intCardID = 0;
            intCardNum = PanGroup.Count;

            CardID = new Random();

            TCardScroll.Enabled = false;
        }

        private void TCardScrollView(object sender, EventArgs e)
        {
            lblCardScroll.Text = NewCard();
        }

        private String NewCard()
        {
            intCardID = CardID.Next(0, PanGroup.Count - 1);

            return (String)PanGroup[intCardID];
        }

        private String NewPrize()
        {
            String PrizeCard;
            PrizeCard = NewCard();

            OleDbConnection Conn = new OleDbConnection(Properties.Settings.Default.strOleConn);
            OleDbCommand Comm = Conn.CreateCommand();

            Conn.Open();
            Comm.CommandText = "insert into TPrizes (EventID,PrizeName,PrizeID,Pan) values(1,'三等奖',3,'" + PrizeCard + "')";
            Comm.ExecuteNonQuery();

            Comm.Dispose();
            Conn.Close();
            Conn.Dispose();

            PrizeGroup.Add(PrizeCard);
            PanGroup.Remove(PrizeCard);
            intState++;
            return PrizeCard;
        }
    }
}
